package com.bjtech.web.action.management;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.ssi.util.WFDOMUtils;

import com.bjtech.dao.AssignMeterDataDao;
import com.bjtech.dao.BaseShareAreaInfoDao;
import com.bjtech.dao.SysAccessaryDao;
import com.bjtech.domain.AssignMeterData;
import com.bjtech.domain.BaseShareAreaInfo;
import com.bjtech.domain.query.AssignMeterDataQuerySpec;
import com.bjtech.domain.query.BaseShareAreaInfoQuerySpec;
import com.bjtech.web.action.LyheatBaseAction;
import com.bjtech.web.util.ReadAssignMeterData;

public class AssignMeterDataAction extends LyheatBaseAction {

	private static final long serialVersionUID = 1L;
	private AssignMeterDataDao assignMeterDataDao;
	private BaseShareAreaInfoDao baseShareAreaInfoDao;
	private AssignMeterDataQuerySpec selectBean;
	
	private SysAccessaryDao sysAccessaryDao;
	private String custom;
	private String xmlStr;
	
	private List shareAreaList;
	private List dataList;
	
	private String jzqNum;
	
	public void prepare() throws Exception {

	}
	
	public AssignMeterDataAction() throws Exception{
		model = new AssignMeterData();
		selectBean = new JzqDataQuerySpec();
	}
	
	public void setAssignMeterDataDao(AssignMeterDataDao assignMeterDataDao) {
		this.assignMeterDataDao = assignMeterDataDao;
	}

	public String execute(){
		return this.list();
	}
	
	public String list(){
		shareAreaList = baseShareAreaInfoDao.selectEntityList(new BaseShareAreaInfo(), new BaseShareAreaInfoQuerySpec());
		selectBean.queryParamString();
		list = this.assignMeterDataDao.selectEntityPaginatedList(new AssignMeterData(),selectBean);
		//list = this.assignMeterDataDao.selectJzqTjInfo(jzqNum);
		return "list";
	}
	
	public String listAll(){
		custom = "AssignMeterData/listAll.jsp";
		selectBean.queryParamString();
		list = this.assignMeterDataDao.selectJzqDataInfo(selectBean);
		return "custom";
	}
	
	public String add(){
		return "edit";
	}
	
	public String save(){
		AssignMeterData entity = (AssignMeterData)model;
		if(entity.getId()==null){
			assignMeterDataDao.insertEntity(entity);
		}else{
			assignMeterDataDao.updateEntity(entity);
		}
		return listAll();
	}
	
	public String edit(){
		model = this.assignMeterDataDao.selectEntity(new AssignMeterData(),id);
		return "edit";
	}

	public String view(){
	    //model = this.assignMeterDataDao.selectEntity(new AssignMeterData(),id);
	//	selectBean.setJzq_num(((AssignMeterData)model).getJzq_num());
		selectBean.queryParamString();
		dataList = this.assignMeterDataDao.selectEntityList(new AssignMeterData(),selectBean);
		return "view";
	}
	
	public String delete(){
		model = this.assignMeterDataDao.deleteEntity(new AssignMeterData(),id);
		return "success";
	}
	
	public String uploadMF(){
		custom = "AssignMeterData/upload.jsp";
		return "custom";
	}
	
	public void importData(){
		String xml = readXmlFormRequestBody(ServletActionContext.getRequest());
		try {
			List<AssignMeterData> dataList = new WFDOMUtils().parseMeterDataXml(xml);
			this.assignMeterDataDao.insertSjMeterDatas(dataList,-1l);//-1表示手机上传
		} catch (Exception e1) {
			e1.printStackTrace();
		}
//		xmlStr = xml;
//		System.out.println(xmlStr);
//	    this.getResponse().setContentType("text/xml");
//	    
//	    try {
//			this.getResponse().setContentType("text/xml;charset=utf-8");   
//			this.getResponse().setHeader("Cache-Control", "no-cache");   
//			this.getResponse().getWriter().write("上传成功");  
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
	}
	
	private String readXmlFormRequestBody(HttpServletRequest request) {
		StringBuffer sb_xml = new StringBuffer();
		String row = "";
		
		BufferedReader br;
		try {
			br = new BufferedReader(new InputStreamReader((ServletInputStream)request.getInputStream()));
			while((row = br.readLine())!=null){
				sb_xml.append(row);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sb_xml.toString();
	}

	public void readGPRSData(){
		new ReadAssignMeterData().readData();
	}

	public AssignMeterDataQuerySpec getSelectBean() {
		return selectBean;
	}

	public void setSelectBean(AssignMeterDataQuerySpec selectBean) {
		this.selectBean = selectBean;
	}

	public SysAccessaryDao getSysAccessaryDao() {
		return sysAccessaryDao;
	}

	public void setSysAccessaryDao(SysAccessaryDao sysAccessaryDao) {
		this.sysAccessaryDao = sysAccessaryDao;
	}

	public String getCustom() {
		return custom;
	}

	public void setCustom(String custom) {
		this.custom = custom;
	}

	public AssignMeterDataDao getAssignMeterDataDao() {
		return assignMeterDataDao;
	}

	public String getXmlStr() {
		return xmlStr;
	}

	public void setXmlStr(String xmlStr) {
		this.xmlStr = xmlStr;
	}

	public List getShareAreaList() {
		return shareAreaList;
	}

	public void setShareAreaList(List shareAreaList) {
		this.shareAreaList = shareAreaList;
	}

	public BaseShareAreaInfoDao getBaseShareAreaInfoDao() {
		return baseShareAreaInfoDao;
	}

	public void setBaseShareAreaInfoDao(BaseShareAreaInfoDao baseShareAreaInfoDao) {
		this.baseShareAreaInfoDao = baseShareAreaInfoDao;
	}

	public List getDataList() {
		return dataList;
	}

	public void setDataList(List dataList) {
		this.dataList = dataList;
	}

	public String getJzqNum() {
		return jzqNum;
	}

	public void setJzqNum(String jzqNum) {
		this.jzqNum = jzqNum;
	}
}
